package com.example.demo.config;


import com.example.demo.interceptor.CorsInterceptor;
import com.example.demo.interceptor.JWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
//    @Value("${prop.upload-folder}")
//    private String UPLOAD_FOLDER;
@Resource
private CorsInterceptor corsInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        registry.addInterceptor(new JWTInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/User/login/*")
                .excludePathPatterns("/User/login")
                .excludePathPatterns("swagger/**")
                .excludePathPatterns("swagger-ui.html")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/swagger-ui.html/*")
                .excludePathPatterns("/swagger-resources")
                .excludePathPatterns("/swagger-resources/**");

//        swagger/**,/swagger-ui.html,swagger-ui.html,/webjars/**,/swagger-ui.html/*,/swagger-resources,/swagger-resources/**
//                .excludePathPatterns("/img/**").excludePathPatterns("/**");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//        registry.addResourceHandler("/img/**").addResourceLocations("file:" + UPLOAD_FOLDER);
    }
}
